package com.pfzy.utildemo.activity.record.recording.record;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;

import com.pfzy.lib.config.log.MyLog;
import com.pfzy.utildemo.activity.record.recording.DTO.UXDROrderInfo;
import com.pfzy.utildemo.activity.record.recording.DTO.UXDRRecord;
import com.pfzy.utildemo.activity.record.recording.UXDriverRecodingParams;
import com.pfzy.utildemo.activity.record.soundrecord.RecordingInfo;
import com.pfzy.utildemo.activity.record.soundrecord.SoundRecordCallback;
import com.pfzy.utildemo.activity.record.soundrecord.UxSoundRecordApi;
import com.pfzy.utildemo.activity.record.soundrecord.constatns.CallbackConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * 司机录制
 */
public class DriverRecording {
    private static final int TWO_MINUTE = 120000;
    private Context mContext;
    private String mOrderNo;
    private UXDriverRecodingParams mRecodingParams;
    private HashMap<String, UXDRRecord> mRecordHashMap = new HashMap<>();
    private HashMap<String, UXDROrderInfo> mRequestHashMap = new HashMap<>();

    public void initParams(Context context, UXDriverRecodingParams recodingParams) {
        if (this.mContext == null) {
            this.mContext = context.getApplicationContext();
            UxSoundRecordApi.getInstance(this.mContext).init(recodingParams.getBaseUrl());
        }
        this.mRecodingParams = recodingParams;
    }

    /**
     * 开始录音
     */
    public void startRecord(Activity activity) {
        if (UxSoundRecordApi.getInstance(this.mContext).isRecording()) {
            _logd("订单开启录音（当前已在录音，不用再次开启）");
            return;
        }

        RecordingInfo recordingInfo = new RecordingInfo(1, "1", 60, "1");
        if (this.mRecodingParams.getSplitTimeIntervalMs() > 0) {
            recordingInfo.setSplitTimeIntervalMs(this.mRecodingParams.getSplitTimeIntervalMs());
        } else {
            recordingInfo.setSplitTimeIntervalMs(TWO_MINUTE);
        }
        _logd("调用开启录音");

        UxSoundRecordApi.getInstance(this.mContext).start(recordingInfo, false, activity, new SoundRecordCallback() {
            @Override
            public void onStart(RecordingInfo info) {
                _logd("开启录音成功");
            }

            @Override
            public void onStop(RecordingInfo info, Map<String, String> map) {
                String error;
            }

            @Override
            public void onFail(RecordingInfo info, String failInfo) {
                _logd("录音失败   " + failInfo);
            }
        });
    }

    /**
     * 停止录制
     */
    public void stopRecording() {
        this.mRequestHashMap.clear();
        this.mRecordHashMap.clear();
        this.mOrderNo = null;
        UxSoundRecordApi.getInstance(this.mContext).stop(null);
    }

    /**
     * 停止服务
     */
    public void stopService() {
        if (this.mRecordHashMap.size() == 0) {
            _logd("没有订单，录音关闭");
            UxSoundRecordApi.getInstance(this.mContext).stop(null);
            return;
        }
        Set<String> strings = this.mRecordHashMap.keySet();
        boolean stop = true;
        Iterator<String> it = strings.iterator();
        while (true) {
            if (!it.hasNext()) {
                break;
            }
            String string = it.next();
            UXDRRecord uxdrRecord = this.mRecordHashMap.get(string);
            if (uxdrRecord.getCanRecord() == 1) {
                stop = false;
                break;
            }
        }
        if (stop) {
            _logd("剩下订单不需要录音，录音关闭 " + this.mRecordHashMap);
            UxSoundRecordApi.getInstance(this.mContext).stop(null);
            return;
        }
        _logd("剩下订单需要录音，继续录音 " + this.mRecordHashMap);
    }

    private void _logd(String msg) {
        MyLog.logd("司机录音 " + msg);
    }

    private void _loge(String msg) {
        MyLog.loge("司机录音 " + msg);
    }
}
